#include <ximera/gui/cegui/listbox.hpp>

namespace ximera { namespace gui { namespace cegui
{
    UInt32 Listbox::itemCount() const
    {
        return (UInt32)ceguiListbox()->getItemCount();
    }

    void Listbox::addItem(String const& text)
    {
        ceguiListbox()->addItem(new CEGUI::ListboxTextItem(string_util::wstr2str(text)));
    }

    void Listbox::removeItem(UInt32 index)
    {
        if (index >= itemCount())
            XIMERA_THROW(InvalidParamException, L"index is out of range!");

        CEGUI::ListboxItem* item = ceguiListbox()->getListboxItemFromIndex(index);
        if (item)
            ceguiListbox()->removeItem(item);
    }

    void Listbox::removeAllItems()
    {
        ceguiListbox()->resetList();
    }

    Int32 Listbox::selectedIndex() const
    {
        CEGUI::ListboxItem* item = ceguiListbox()->getFirstSelectedItem();
        if (!item)
            return -1;

        return (UInt32)ceguiListbox()->getItemIndex(item);
    }
}}}

